#include "Expand/Plugins/GameModePlugin.h"
#include "Player/PlayerData.h"
#include "Subsystems/SubsystemPlayers.h"
#include "Network/Client.h"

#include "Log.h"

#include "GameSingleton.h"

namespace PocketSurvival
{
    const std::string &GameModePlugin::getPluginName()
    {
        static const std::string pluginName = "游戏模式插件";
        return pluginName;
    }

    const std::string &GameModePlugin::getIntroduce()
    {
        static const std::string introduce = "\n/gamemode user (在线玩家) (模式)\n/gamemode guid (在线玩家的guid) (模式)\n模式: creative-创造  harmless-无害  follow-跟随服务器";
        return introduce;
    }

    uint32_t GameModePlugin::getUseAuthLevel()
    {
        return 100;
    }

    AuthDisplayType GameModePlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void GameModePlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if(cmdDataVec.size() >= 4)
        {
            if(cmdDataVec[1] == "user")
            {
                std::vector<std::shared_ptr<PlayerData>> playerVec;
                GameSingleton::singletonPtr->getSingleton<SubsystemPlayers>()->getPlayerDataByName(cmdDataVec[2], playerVec);
                uint32_t findCount = playerVec.size();
                if (findCount > 1)
                {
                    chatObj.sendMessage("gamemode", fmt::format("存在多个同名 {} 的玩家，需要手动使用GUID修改模式", cmdDataVec[2]));
                }
                else if (findCount > 0)
                {
                    std::shared_ptr<PlayerData> targetPlayer = playerVec[0];
                    std::string modeName;
                    if(cmdDataVec[3] == "creative")
                    {
                        targetPlayer->playerGameMode = PlayerGameMode::Creative;
                        modeName = "创造模式";
                    }
                    else if(cmdDataVec[3] == "harmless")
                    {
                        targetPlayer->playerGameMode = PlayerGameMode::Harmless;
                        modeName = "无害模式";
                    }
                    else if(cmdDataVec[3] == "follow")
                    {
                        targetPlayer->playerGameMode = PlayerGameMode::Follow;
                        modeName = "默认";
                    }
                    else
                    {
                        chatObj.sendMessage("gamemode", getIntroduce());
                        return;
                    }
                    chatObj.sendMessage("gamemode", fmt::format("成功将 {} 的游戏模式改为: {}", cmdDataVec[2], modeName));
                    targetPlayer->client->disconnect(fmt::format("管理员将你的游戏模式改为 {}", modeName));
                }
                else
                {
                    chatObj.sendMessage("gamemode", fmt::format("玩家 {} 不在线", cmdDataVec[2]));
                }
                return;
            }

            if(cmdDataVec[1] == "guid")
            {
                GameGuid gameGuid(cmdDataVec[2]);
                std::shared_ptr<PlayerData> targetPlayer = GameSingleton::singletonPtr->getSingleton<SubsystemPlayers>()->getPlayerDataByGuid(gameGuid);
                if(targetPlayer != nullptr)
                {
                    std::string modeName;
                    if(cmdDataVec[3] == "creative")
                    {
                        targetPlayer->playerGameMode = PlayerGameMode::Creative;
                        modeName = "创造模式";
                    }
                    else if(cmdDataVec[3] == "harmless")
                    {
                        targetPlayer->playerGameMode = PlayerGameMode::Harmless;
                        modeName = "无害模式";
                    }
                    else if(cmdDataVec[3] == "follow")
                    {
                        targetPlayer->playerGameMode = PlayerGameMode::Follow;
                        modeName = "默认";
                    }
                    else
                    {
                        chatObj.sendMessage("gamemode", getIntroduce());
                        return;
                    }
                    chatObj.sendMessage("gamemode", fmt::format("成功将 {} 的游戏模式改为: {}", targetPlayer->username, modeName));
                    targetPlayer->client->disconnect(fmt::format("管理员将你的游戏模式改为 {}", modeName));
                }
                else
                {
                    chatObj.sendMessage("gamemode", fmt::format("GUID为 {} 的玩家貌似不在线", cmdDataVec[2]));
                }
                return;
            }
        }
        chatObj.sendMessage("gamemode", getIntroduce());
    }

    void GameModePlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/gamemode");
    }

} // namespace PocketSurvival
